package net.kunyv.base.service;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.kunyv.base.mapper.SysCodeMapper;
import net.kunyv.base.entity.SysCompany;
import net.kunyv.base.mapper.SysCompanyMapper;
import net.kunyv.util.PageParam;
import net.kunyv.util.PageResult;
import net.kunyv.util.SortParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 公司表Service
 *
 * @author sunyb
 * @date 2020-07-07
 */
@Service
public class CorpManageService extends BaseService<SysCompany> {
    @Autowired
    private SysCodeMapper sysCodeMapper;
    @Autowired
    private SysCompanyMapper sysCompanyMapper;

    /**
     * 获取公司表维护datagrid数据
     *
     * @param fldEng
     * @param code
     * @param pageParam
     * @return net.kunyv.bde.ssm.PageResult
     * @throw
     * @author sunyb
     * @date 2020/7/7
     */
    public PageResult getSysCodeGridData(String fldEng, String code, PageParam pageParam, SortParam sortParam) {
        Example example = new Example(SysCompany.class);
        //设置排序
        example.setOrderByClause(sortParam.getSort() + " " + sortParam.getOrder());
        //拼接sql
        Example.Criteria criteria = example.createCriteria();
        /**/
        if (StringUtils.isNotEmpty(fldEng)) {
//            criteria.andEqualTo("companyName", fldEng);
            criteria.andLike("companyName", "%" + fldEng.toUpperCase() + "%");
        }
        if (StringUtils.isNotEmpty(code)) {
            criteria.andLike("companyNature", "%" + code.toUpperCase() + "%");
        }

        //分页
        Page page = PageHelper.startPage(pageParam.getPage(), pageParam.getRows());


        //List<SysCompany> sysCorpList = null;
        List<SysCompany> sysCorpList = sysCompanyMapper.selectByExample(example);


        PageResult pageResult = new PageResult();
        pageResult.setRows(sysCorpList);
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    /**
     * 获取代码项combobox下拉数据
     *
     * @param q
     * @return java.util.List<java.lang.String>
     * @throw
     * @author sunyb
     * @date 2020/7/10
     */
    public List<HashMap> getSysCodeList(String q) {
        return sysCompanyMapper.getSysCodeList(q);
    }

    /**
     * 新增或编辑
     *
     * @param sysCompany
     * @param userId
     * @return java.lang.String
     * @throw
     * @author sunyb
     * @date 2020/7/7
     */
    public String save(SysCompany sysCompany, String userId) {
        Date now = new Date();

        if (StringUtils.isEmpty(sysCompany.getCompanyId())) {
            sysCompany.setCompanyId(null);
            Example example = new Example(SysCompany.class);
            Example.Criteria criteria = example.createCriteria();
            /*
              criteria.andEqualTo("fldEng", sysCompany.getFldEng());
            criteria.andEqualTo("code", sysCompany.getCode());
            * */

            // List<SysCompany> list = sysCompanyMapper.selectByExample(example);
            /*
            if (list.size() > 0) {
                return "该类型代码下CODE已存在";
            } else {

                sysCompany.setCompanyId(null);
                int i = sysCompanyMapper.insert(sysCompany);
                if (i == 1) {
                    return "ok";
                } else {
                    return "新增失败";
                }
            }
            */
            sysCompany.setCompanyId(null);
            int i = sysCompanyMapper.insert(sysCompany);
            if (i == 1) {
                return "ok";
            } else {
                return "新增失败";
            }

        } else {
            Example example = new Example(SysCompany.class);
            Example.Criteria criteria = example.createCriteria();
            /*
            criteria.andEqualTo("fldEng", sysCompany.getFldEng());
            criteria.andEqualTo("code", sysCompany.getCode());
            criteria.andNotEqualTo("id", sysCompany.getId());

            List<SysCompany> list = sysCompanyMapper.selectByExample(example);
            if (list.size() > 0) {
                return "该类型代码下CODE已存在";
            } else {
                int i = sysCompanyMapper.updateByPrimaryKeySelective(sysCompany);
                if (i == 1) {
                    return "ok";
                } else {
                    return "更新失败";
                }
            } */

            int i = sysCompanyMapper.updateByPrimaryKeySelective(sysCompany);
            if (i == 1) {
                return "ok";
            } else {
                return "更新失败";
            }

        }
    }
}
